package com.atguigu.gmall.order.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderReceiver {

    @Autowired
    private OrderService orderService;

    /**
     * 超时取消订单
     */
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    @SneakyThrows
    public void cancelOrder(Long orderId, Message message, Channel channel){

        try {
            //判断id
            if(orderId!=null){

                //判断状态--查询订单信息
                OrderInfo orderInfo = orderService.getById(orderId);
                //判断
                if(orderInfo!=null
                        &&"UNPAID".equals(orderInfo.getOrderStatus())
                        &&"UNPAID".equals(orderInfo.getProcessStatus())){

                    //修改订单状态
                    orderService.execExpireOrder(orderId);

                }


            }
        } catch (Exception e) {
            //记录日志，通知运营人员。。。发送短信给码农
            e.printStackTrace();
        }


        //确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
